package queue

import (
	C "gitee.com/ljfirst/algo-go-sdk/common/constant"
	"gitee.com/ljfirst/algo-go-sdk/src/data_structure/list"
)

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/6/27 01:01
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description
 * */
type LinkedQueue struct {
	size    int
	maxSize int
	front   *list.Node
	tail    *list.Node
}

func NewLinkedQueue() *LinkedQueue {
	return &LinkedQueue{
		size:    0,
		maxSize: 8,
		front:   nil,
		tail:    nil,
	}
}

func (m *LinkedQueue) Offer(value int) {
	if m.size == m.maxSize {
		m.Resize()
	}
	node := &list.Node{Value: value}
	if m.size == 0 {
		m.front = node
	} else {
		m.tail.Next = node
	}
	m.tail = node
	m.size++
}

func (m *LinkedQueue) Poll() int {
	if m.size == 0 {
		return C.ErrorNum
	}
	temp := m.front
	m.front = m.front.Next
	m.size--
	return temp.Value.(int)
}

func (m *LinkedQueue) Peak() int {
	if m.size == 0 {
		return C.ErrorNum
	}
	return m.front.Value.(int)
}

func (m *LinkedQueue) Size() int {
	return m.size
}

func (m *LinkedQueue) Resize() {
	m.maxSize *= 2
}

func (m *LinkedQueue) StructName() string {
	return "LinkedQueue"
}
